<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :append-to-body="true"
      title="消息表信息"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-form
        ref="formData"
        class="base-pipeline-layout system-user-form"
        label-width="100px"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          label="消息类型"
          :label-width="formLabelWidth"
          prop="messageType"
          :error="errorMsg.messageType"
        >
          <el-input
          v-model="formData.messageType"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="业务编号"
          :label-width="formLabelWidth"
          prop="businessNum"
          :error="errorMsg.businessNum"
        >
          <el-input
          v-model="formData.businessNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="提醒内容"
          :label-width="formLabelWidth"
          prop="content"
          :error="errorMsg.content"
        >
          <el-input
          v-model="formData.content"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="提醒状态(1:启用;0:禁用)"
          :label-width="formLabelWidth"
          prop="statusCode"
          :error="errorMsg.statusCode"
        >
          <el-input
          v-model="formData.statusCode"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit('formData')"
          >保 存</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/Message/MessageInfo.js";

export default {
  name: "MessageInfoEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        messageType: 0,
        businessNum: "",
        content: "",
        statusCode: 0,
      },
      formData: {
        id: 0,
        messageType: 0,
        businessNum: "",
        content: "",
        statusCode: 0,
      },
      formDataRules: {
        messageType: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        businessNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        content: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        statusCode: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
      },
      initErrorMsg: {
        messageType: "",
        businessNum: "",
        content: "",
        statusCode: "",
      },
      errorMsg: {
        messageType: "",
        businessNum: "",
        content: "",
        statusCode: "",
      },
    };
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("messageType", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("businessNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("content", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("statusCode", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
